﻿using MeterialManager.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MeterialManager
{
    public partial class FrmStatistics : Form
    {
        public FrmStatistics()
        {
            InitializeComponent();
        }

        private void FrmStatistics_Load(object sender, EventArgs e)
        {
            BindDgv();
        }

        /*public void BindCbx()
        {
            List<Category> categories = new List<Category>();
            categories = Category.GetListCategory();
            categories.Insert(0, new Category()
            {
                CategoryId = 0,
                CategoryCode = "",
                CategoryName = "-查询所有-"
            });
            cbxCategoryName.DataSource = categories;
            cbxCategoryName.DisplayMember = "CategoryName";
            cbxCategoryName.ValueMember = "CategoryId";
        }*/

        public void BindDgv()
        {
            //int CategoryId = (int)cbxCategoryName.SelectedValue;
            List<MeterialCategory> meterialCategories = MeterialCategory.ListAll();
            dgvMeterialCategory.DataSource = (from u in meterialCategories
                                              group u by u.CategoryName into g
                                              orderby g.Sum(p => p.Stocks) descending
                                              select new
                                              {
                                                  CategoryName = g.Key,
                                                  Stocks = g.Sum(p => p.Stocks)
                                              }).ToList();
            /*if (CategoryId == 0)
            {
                dgvMeterialCategory.DataSource = (from u in meterialCategories
                                                  orderby u.Stocks descending
                                                 select new { 
                                                    u.Id,
                                                    u.CategoryName,
                                                    u.MeterialCode,
                                                    u.MeterialName,
                                                    u.Unit,
                                                    u.Stocks,
                                                    u.Remark
                                                 }).ToList();
            }
            else
            {

                dgvMeterialCategory.DataSource = (from u in meterialCategories
                                                  where u.CategoryId == CategoryId
                                                  orderby u.Stocks descending
                                                  select new
                                                  {
                                                      u.Id,
                                                      u.CategoryName,
                                                      u.MeterialCode,
                                                      u.MeterialName,
                                                      u.Unit,
                                                      u.Stocks,
                                                      u.Remark
                                                  }).ToList();
            }*/
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            BindDgv();
        }

        private void FrmStatistics_Enter(object sender, EventArgs e)
        {
            BindDgv();
        }
    }
}
